package com.ericsson.enap.modules.backup.utils;

import com.ericsson.common.pojo.FTPInfoVO;
import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.common.util.SFtpCarrierUtilNew;
import com.ericsson.enap.modules.sys.entity.EnipNetelement;
import com.jcraft.enipjsch.ChannelSftp;
import com.jcraft.enipjsch.SftpATTRS;
import com.jcraft.enipjsch.SftpException;
import org.slf4j.Logger;

import java.io.*;
import java.util.Iterator;
import java.util.Vector;

/**
 * @description: epc备份工具类
 * @author: wangjingshi
 * @create: 2021-09-03 10:34
 */
public class VgcBackupUtil {

    private static Logger log = LoggerUtils.Logger(LogFileName.VgcDataBackupJob);

    /**
     * sftp下载文件
     * @param net
     * @param filePath
     * @param ftpPath
     * @param bizName
     */
    public static void downloadLogBySftp(EnipNetelement net, String filePath, String ftpPath, String bizName, Logger log) {
        FTPInfoVO ftpInfo = new FTPInfoVO();
        ftpInfo.setHost(net.getIp4());
        ftpInfo.setUserName(net.getUsername());
        ftpInfo.setPassword(net.getPassword());
        ftpInfo.setPath(ftpPath);
        ftpInfo.setPort(21);
        //System.out.println("--------------------- ftpInfo:" + ftpInfo);
        SFtpCarrierUtilNew sftpCarrier = new SFtpCarrierUtilNew(ftpInfo, log);
        try {
            sftpCarrier.connect();
            System.out.println("--------------------- connect success -----------------");
            Vector v = sftpCarrier.listFiles(ftpInfo.getPath());
            if (v.size() > 0) {
                Iterator it = v.iterator();
                while (it.hasNext()) {
                    ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) it.next();
                    String filename = entry.getFilename();
                    SftpATTRS attrs = entry.getAttrs();
                    if (!attrs.isDir()) {
                        if (filename.contains(bizName)&& net.getType() == 1) {
                            log.info(" ==== filename:"+filename);
                            sftpCarrier.downloadFile(ftpInfo.getPath(), filename, filePath +"/"+bizName+"/", filename);
                        }
                        if ((filename.startsWith("auth.log")||"audit.log".equals(filename)||"ericsson.xml".equals(filename)) &&
                                ((net.getType()==2)||(net.getType()==3))) {
                            log.info(" ==== filename:"+filename);
                            sftpCarrier.downloadFile(ftpInfo.getPath(), filename, filePath +"/"+bizName+"/", filename);
                        }
                    }
                }
            }
        } catch (SftpException e) {
            log.error(e.getMessage(), e);
        } finally {
            sftpCarrier.disconnect();
        }
    }



    /**
     * 写入新文件
     * @param fileName
     * @param result
     * @param path
     */
    public static void writeNewFile(String fileName, String result, String path) {
        File file = new File(path+fileName);
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(result);
            fw.flush();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 执行压缩命令
     * @param cmd2
     * @param netName
     * @param localFile
     */
    public static void executeZipCmd(String cmd2, String netName, File localFile) {
        Process process = null;
        String command[] = new String[3];
        command[0] = "sh";
        command[1] = "-c";
        command[2] = cmd2;
        log.info(netName+":"+cmd2);
        try {
            process = Runtime.getRuntime().exec(command,null,localFile);
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while((line = br.readLine())!=null){
                //log.info(netName+" ===== "+line);
                //System.out.println(netName+" ===== "+line);
            }
            br.close();
            process.waitFor();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            process.destroy();
        }
    }
}
